package com.liaozibo.demo.seckill.web.controller;

import com.liaozibo.demo.seckill.support.export.dto.ActivityDTO;
import com.liaozibo.demo.seckill.support.export.dto.Result;
import com.liaozibo.demo.seckill.support.export.service.ActivityExportService;
import com.liaozibo.demo.seckill.web.model.ActivityDescDTO;
import com.liaozibo.demo.seckill.web.util.ResultUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 活动数据查询属于秒杀系统
 * 活动管理属于运营系统
 * */
@RequestMapping("/activity")
@RestController
public class ActivityController {

    @DubboReference
    private ActivityExportService activityExportService;

    /**
     * 活动数据查询接口
     * */
    @GetMapping("/activityData")
    @CrossOrigin
    public Result<ActivityDescDTO> activityData(@RequestParam String code) {
        Result<ActivityDTO> activityDTOResult = activityExportService.getActivityByCode(code);
        if (ResultUtils.isNull(activityDTOResult)) {
            return Result.fail();
        }
        ActivityDTO activityDTO = activityDTOResult.getData();
        ActivityDescDTO activityDescDTO = new ActivityDescDTO();
        BeanUtils.copyProperties(activityDTO, activityDescDTO);
        String status = "";
        if (activityDTO.getStatus() == ActivityDTO.STATUS_NOT_START) {
            status = "未开始";
        } else if (activityDTO.getStatus() == ActivityDTO.STATUS_START) {
            status = "进行中";
        } else if (activityDTO.getStatus() == ActivityDTO.STATUS_END) {
            status = "已结束";
        }
        activityDescDTO.setStatus(status);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        activityDescDTO.setStartTime(activityDTO.getStartTime().format(formatter));
        activityDescDTO.setEndTime(activityDTO.getEndTime().format(formatter));
        return Result.ok(activityDescDTO);
    }


    /**
     * 创建秒杀活动
     * */
    @PostMapping("/createActivity")
    public Result createActivity() {
        LocalDateTime now = LocalDateTime.now();
        ActivityDTO activityDTO = new ActivityDTO();
        activityDTO.setName("荣耀v40秒杀活动");
        activityDTO.setCode("1000");
        activityDTO.setPrice(99800);
        activityDTO.setLimit(2);
        activityDTO.setStock(4);
        activityDTO.setStartTime(now);
        activityDTO.setEndTime(now.plusDays(2));
        activityDTO.setStatus(ActivityDTO.STATUS_NOT_START);
        return activityExportService.createActivity(activityDTO);

    }

    /**
     * 开始秒杀活动（手动）
     * 可由定时任务自动关闭
     * */
    @PostMapping("/startActivity")
    public Result startActivity(@RequestBody ActivityDTO activityDTO) {
        return activityExportService.startActivity(activityDTO.getCode());
    }

    /**
     * 结束秒杀活动（手动）
     * 可由定时任务自动关闭
     * */
    @PostMapping("/endActivity")
    public Result endActivity(@RequestBody ActivityDTO activityDTO) {
        return activityExportService.endActivity(activityDTO.getCode());
    }
}
